<?php
class ProfilPeer extends BaseProfilPeer
{
  public static $tip = array('administrator' => 1, 'upravnik' => 2, 'profesor' => 3, 'roditelj' => 4, 'ucenik' => 5, 'menadzer' => 6, 'operater' => 7);
  public static $pol = array('z' => 0, 'm' => 1);

  public static function getTips()
  {
    return array_flip(self::$tip);
  }

  public static function getPols()
  {
    return array_flip(self::$pol);
  }

  public static function createRandomPassword()
  {
    $chars = "abcdefghijkmnopqrstuvwxyz023456789";
    srand((double)microtime()*1000000);
    $i = 0;
    $pass = '' ;
    while ($i <= 7)
    {
      $num = rand() % 33;
      $tmp = substr($chars, $num, 1);
      $pass = $pass . $tmp;
      $i++;
    }
    return substr($pass, 0, 6);

  }

  public static function defaultUsername(Profil $profil)
  {
    $user_default = Functions::kvacice(trim(str_replace(' ', '', $profil->getPrezime())), 1).substr(Functions::kvacice(trim(str_replace(' ', '', $profil->getIme())), 1), 0, 1);
    $user = $user_default;
    $broj = 1;
    while(1)
    {
      $conn = Propel::getConnection();
      $query = "SELECT count(id) AS nbrows FROM profil WHERE username='$user'";
      $statement = $conn->prepare($query);
      $statement->execute();
      $record = $statement->fetch(PDO::FETCH_OBJ);
      $nbrows = $record->nbrows;

      if($nbrows == 0)
        return $user;
      else
      {
        $user = $user_default.$broj;
        $broj ++;
      }
    }

    return $user;
  }

}